URL = "http://www.pyside.org/files/pyside-tools-%(VERSION)s.tar.bz2" 
URL = "https://distfiles.macports.org/py-pyside-tools/pyside-tools-%(VERSION)s.tar.bz2"

VERSIONS = ["0.2.8", "0.2.9", "0.2.13"]
DEFAULT_VERSION = "0.2.13"
DEPENDS = ["jom", "qt4osmin", "pyside" ]

# TODO Fix backward compatibility.
# pyside-1.1.1 depends on qt-4.8.0.
# pyside-1.1.1 depends on shiboken-1.1.1
# pyside-1.1.1 is coupled with pyside_tools-0.2.13.
#
# Since pyside-1.1.1, dependencies to apiextractor and generatorrunner
# have been removed.
#
# Older versions of pyside (1.0.*) may no longer work because of this dep.


from source.builders import CMakeBuilder
import os

class pysidetoolsBuilder( CMakeBuilder ):
  """ subclass of CMakeBuilder with custom configure step """

  def setgenerator(self):
    """default generator using machine.buildarch"""
    if self.machine.isLinux():
      CMakeBuilder.setgenerator(self)
    else:
      self.generator = "NMake Makefiles"

  def hasdebug(self):
      return False;

  def getpythonpath (self):
    return os.path.join(self.targetpath(), "lib")

  def process(self ):
      return None if self.debug else CMakeBuilder.process(self)
  def configure(self):
    if "jom" in self.dependencies.keys(): 
      self.dependencies["jom"].addexetopath()  
    qtdir = self.dependencies['qt4osmin'].targetpath()
    os.environ['QTDIR'] = qtdir
    os.environ['PySide_DIR'] = self.dependencies['pyside'].targetpath()
    
    pysidedlldir = os.sep.join([self.extractedpath,"libpyside",("Release","Debug")[self.debug]])

    #put our qt in the start of the path, else another qt dll, possible 32-bit will prevent generatorrunner from running
    os.environ['PATH'] =  os.pathsep.join([os.environ['QTDIR']+os.sep+"bin",
                                           pysidedlldir,
                                           os.environ['PATH']])
    if self.machine.isLinux():
      os.environ['LD_LIBRARY_PATH'] =  os.pathsep.join([os.environ['QTDIR']+os.sep+"lib",
                                           os.environ['LD_LIBRARY_PATH'] if 'LD_LIBRARY_PATH' in os.environ.keys() else "" ])

    #define PYTHON_DEBUG_LIBRARY, PYTHON_EXECUTABLE, PYTHON_INCLUDE_DIR, PYTHON_LIBRARY
    pythonbindir= self.dependencies["python"].getbindir()
    pythonlibdir =  self.dependencies["python"].getlibdir() + ("s", "" )[self.machine.isLinux()]
    pythonincludedir = self.dependencies["python"].getincludedir()

    pyversionshort = self.dependencies["python"].libversionshort()
    pyversion = self.dependencies["python"].libversion()
    pythonlib = ("python%s.lib" % pyversionshort, "libpython%s.so" % pyversion)[self.machine.isLinux()]
    pythondebuglib = ("python%s_d.lib" % pyversionshort, "libpython%s_d.so" % pyversion)[self.machine.isLinux()]

    incdir = " -DPYTHON_INCLUDE_DIR=" + pythonincludedir

    #todo fix getting version from dep
    lib = " -DPYTHON_LIBRARY="+pythonlibdir+os.sep+pythonlib
    libdebug = " -DPYTHON_DEBUG_LIBRARY="+pythonlibdir+os.sep+pythondebuglib

    if 'LIB' in os.environ.keys():
      os.environ['LIB'] = os.pathsep.join([pythonlibdir, os.environ['LIB']])
    else:
      os.environ['LIB'] = pythonlibdir

    if 'INCLUDE' in os.environ.keys():
      os.environ['INCLUDE'] = os.pathsep.join([pythonincludedir, os.environ['INCLUDE']])
    else:
      os.environ['INCLUDE'] = pythonincludedir

    pythonexe = (("python.exe", "python_d.exe")[self.debug], ("python", "python_d")[self.debug])[self.machine.isLinux()]
    exe = " -DPYTHON_EXECUTABLE="+pythonbindir+os.sep+pythonexe

    notests = " -DBUILD_TESTS=FALSE"
    

    CMakeBuilder.configure(self, incdir+lib+libdebug+exe+notests)

BUILDER = pysidetoolsBuilder
